package com.food.restaurantpro.fragments;

import java.util.ArrayList;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.activity.FoodDetailActivity;
import com.food.restaurantpro.activity.MainActivity;
import com.food.restaurantpro.adapters.FoodGridViewAdapter;
import com.food.restaurantpro.adapters.FoodListViewAdapter;
import com.food.restaurantpro.database.tables.FoodTable;
import com.food.restaurantpro.objects.Food;
import com.food.restaurantpro.utils.Prefs;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class FoodGridViewFragment extends Fragment implements OnClickListener {

	private TextView tvHeader;
	private ArrayList<Food> mFoodList;
	private GridView mGridView;
	private FoodGridViewAdapter mFoodGridViewAdapter;
	private Button btnGridView;
	private ListView mListView;
	private FoodListViewAdapter mFoodListViewAdapter;
	private Button btnListView;
	
	//Khi checkVisiblePreOrder > 0 thi visible RelativeLayout Pre-Order
	public static int checkVisiblePreOrder = 0;
	private RelativeLayout mRLPreOrder;

	private Prefs mPrefs;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		checkVisiblePreOrder = 0;
		mPrefs = new Prefs(getActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_food_gridview,
				container, false);
		tvHeader = (TextView) view.findViewById(R.id.tvHeader);
		tvHeader.setText(R.string.tvStarters_Title);
		
		//Khi onbackpressed tu MenuPreOrderFragment thi cho nay se duoc goi
		if(mRLPreOrder != null) {
			mRLPreOrder.setVisibility(View.GONE);
			mRLPreOrder = null;
		}
		mRLPreOrder = (RelativeLayout) view.findViewById(R.id.rl_pre_oder);
		if(checkVisiblePreOrder > 0) {
			mRLPreOrder.setVisibility(View.VISIBLE);
		}
		
		mRLPreOrder.setOnClickListener(this);
		
		mGridView = (GridView) view.findViewById(R.id.grvFood);
		mListView = (ListView) view.findViewById(R.id.lvFood);

		btnGridView = (Button) view.findViewById(R.id.btnGrid);
		btnGridView.setOnClickListener(this);
		btnListView = (Button) view.findViewById(R.id.btnList);
		btnListView.setOnClickListener(this);

		// khi user click vao button listview thi lan sau quay lai man hinh nay
		// cung la listview (gridview cung tuong tu)
		if(mPrefs.getBooleanValue(false, Prefs.KEY_FOOD_FRAGMENT_IS_SHOWING_LISTVIEW)) {
			showListView();
		} else {
			showGridView();
		}
		
		
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				startFoodDetailActivity(position);
			}
		});
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				startFoodDetailActivity(position);
			}
		});
		return view;
	}

	// phuong thuc nay duoc goi truoc khi fragment nay duoc onCreateView
	public void updateFoodListByFoodTypeID(String foodTypeID) {
		mFoodList = FoodTable.getFoodListByFoodTypeID(foodTypeID);
	}

	private void showGridView() {
		//cai nay dung khi click 2 button listview and gridview, nhung sai khi onBackpressed
		/*if (mFoodGridViewAdapter == null && mFoodList.size() > 0) {
			mFoodGridViewAdapter = new FoodGridViewAdapter(getActivity(),
					mFoodList, mRLPreOrder);
			mGridView.setAdapter(mFoodGridViewAdapter);
		} else if(mFoodGridViewAdapter != null) {
			mFoodGridViewAdapter.notifyDataSetChanged();
		}*/
		if(mFoodList == null) {
			return;
		}
		mFoodGridViewAdapter = new FoodGridViewAdapter(getActivity(),
				mFoodList, mRLPreOrder);
		mGridView.setAdapter(mFoodGridViewAdapter);
		
		mGridView.setVisibility(View.VISIBLE);
		btnListView.setVisibility(View.VISIBLE);
		//
		mListView.setVisibility(View.GONE);
		btnGridView.setVisibility(View.GONE);
	}

	private void showListView() {
		//cai nay dung khi click 2 button listview and gridview, nhung sai khi onBackpressed
		/*if (mFoodListViewAdapter == null && mFoodList.size() > 0) {
			mFoodListViewAdapter = new FoodListViewAdapter(getActivity(),
					mFoodList, mRLPreOrder);
			mListView.setAdapter(mFoodListViewAdapter);
		} else if(mFoodListViewAdapter != null) {
			mFoodListViewAdapter.notifyDataSetChanged();
		}*/
		if(mFoodList == null) {
			return;
		}
		mFoodListViewAdapter = new FoodListViewAdapter(getActivity(),
				mFoodList, mRLPreOrder);
		mListView.setAdapter(mFoodListViewAdapter);
		
		mGridView.setVisibility(View.GONE);
		btnListView.setVisibility(View.GONE);
		//
		mListView.setVisibility(View.VISIBLE);
		btnGridView.setVisibility(View.VISIBLE);
	}

	private void startMenuPreOrder() {
		if(mFoodList == null) {
			return;
		}
		//Create new data
		ArrayList<Food> foodList = new ArrayList<Food>();
		for(int i = 0; i < mFoodList.size(); i++) {
			if(mFoodList.get(i).isSelected()) {
				foodList.add(mFoodList.get(i));
			}
		}
		RestaurantApp.getInstance().setFoodList(foodList);
		((MainActivity)getActivity()).showMenuPreOrderFragment();
	}
	
	@Override
	public void onDestroy() {
		// TODO delete image and cache de giai phong bo nho
		super.onDestroy();
		mGridView.setAdapter(null);
		if (mFoodGridViewAdapter != null) {
			mFoodGridViewAdapter.clearMemoryCache();
			mFoodGridViewAdapter = null;
		}
		//
		mListView.setAdapter(null);
		if (mFoodListViewAdapter != null) {
			mFoodListViewAdapter.clearMemoryCache();
			mFoodListViewAdapter = null;
		}
		//
		if (mFoodList != null) {
			mFoodList.clear();
			mFoodList = null;
		}
		checkVisiblePreOrder = 0;
		Runtime.getRuntime().gc();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnList:
			mPrefs.putBooleanValue(true, Prefs.KEY_FOOD_FRAGMENT_IS_SHOWING_LISTVIEW);
			showListView();
			break;
		case R.id.btnGrid:
			mPrefs.putBooleanValue(false, Prefs.KEY_FOOD_FRAGMENT_IS_SHOWING_LISTVIEW);
			showGridView();
			break;
		case R.id.rl_pre_oder:
			startMenuPreOrder();
			break;
		default:
			break;
		}
	}
	
	private void startFoodDetailActivity(int position) {
		Bundle bundle = new Bundle();
		bundle.putString(FoodDetailActivity.KEY_FOOD_URL_IMAGE, mFoodList.get(position).getFoodImage());
		bundle.putString(FoodDetailActivity.KEY_FOOD_DESCRIPTION, mFoodList.get(position).getFoodDescription());
		bundle.putString(FoodDetailActivity.KEY_FOOD_PRICE, mFoodList.get(position).getFoodPrice());
		Intent intent = new Intent(getActivity(), FoodDetailActivity.class);
		intent.putExtras(bundle);
		startActivity(intent);
	}
}
